package cn.mdx.controller;

import cn.mdx.common.beans.ResultBean;
import cn.mdx.common.beans.User;
import cn.mdx.common.tool.MailTool;
import cn.mdx.common.utils.CheckUtil;
import cn.mdx.common.utils.FileUtil;
import cn.mdx.mapper.ResultMapper;
import cn.mdx.pojo.*;
import cn.mdx.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings({"ALL", "AlibabaClassMustHaveAuthor"})
@RestController
@Controller
@CrossOrigin
public class EveryController {

    private static final Logger logger = LoggerFactory.getLogger(EveryController.class);

    @Autowired
    private SkillUserService skillUserService;

    @Autowired
    private VoService voService;

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private FieldService fieldService;

    @Autowired
    private AchievementService achievementService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private NeedUserService needUserService;

    @Autowired
    private ResultService resultService;

    @Autowired
    private MailTool mailTool;

    @Autowired
    private ResultMapper resultMapper;




    /**
     *
     *
     * @return 所有部门/学院
     */
    @GetMapping("/get/colleges")
    public ResultBean getColleges() {
        return new ResultBean<ArrayList<College>>(collegeService.getColleges());
    }

    /**
     *
     * @return 一级专业领域
     */
    @RequestMapping("/get/oneFieldList")
    public ResultBean getFieldList() {
        return new ResultBean<ArrayList<Field>>(fieldService.getFieldList());
    }

    /**
     *
     * @param id 一级专业领域id
     * @return 二级专业领域
     */
    @RequestMapping("/get/twoFieldList")
    public ResultBean getFieldsListByField(Integer id) {
        return new ResultBean<ArrayList<Field>>(fieldService.getFieldsListByField(id));
    }

    @RequestMapping("/get/achievements")
    public ResultBean getAchievements() {
        return new ResultBean(achievementService.getAchievements());
    }


    @PostMapping("/getResultList")
    public ResultBean getResultList(Integer page,Integer id){
        return new ResultBean(resultService.getResultList(page,id));
    }

    @PostMapping("/findResultVo")
    public ResultBean findResultVo(Integer rid){
        return new ResultBean(voService.findResultVO(rid));
    }

    /**
     *
     * @return 所有资源类别
     */
    @RequestMapping("/get/resources")
    public ResultBean getResources() {
        return new ResultBean<ArrayList<Resource>>(resourceService.getResources());
    }

    /**
     * 统计
     * @param cid
     * @param timeClass
     * @return
     */
    @PostMapping("/statistics")
    public ResultBean getBySome(Integer cid,Integer timeClass){
        return new ResultBean( voService.getBySome(cid,timeClass));
    }


    @PostMapping("/email")
    public ResultBean email(HttpSession session, String email) {
        CheckUtil.notEmpty("email","邮箱不能为空");
        String vcode = FileUtil.getRandom(6);
        session.setAttribute("vcode",vcode);
        session.setAttribute("email",email);
        mailTool.sendHtml("验证码", "襄阳对接平台验证码为：" + vcode, email);
        return new ResultBean();
    }

//    @PostMapping("/upload")
//    public ResultBean upload(HttpSession session, MultipartFile pic) throws IOException {
//        logger.info("开始上传");
//        String s = new String("img/1-2/");
//        String ss = null;
//        ss = FileUtil.saveImgFile(session,pic,s);
//        return new ResultBean(ss);
//    }




    @PostMapping("/v4/findPassword")
    public ResultBean findPassword(String email, String password, String vcode, HttpSession session) {
        needUserService.findPassword(email,password,vcode,session);
        return new ResultBean();
    }

    @PostMapping("/v4/login")
    public ResultBean needLogin(HttpSession session,String username, String password) {
        needUserService.login(session,username,password);
        return new ResultBean();
    }

    @PostMapping("/v4/register")
    public ResultBean register(Needuser needuser, String vcode, HttpSession session) {
        needUserService.register(needuser,vcode,session);
        return new ResultBean();
    }

    @PostMapping("/v04/setPassword")
    public ResultBean setNeedPassword(String oldPassword,String newPassword, HttpSession session) {
        needUserService.setPassword(oldPassword,newPassword,User.getId(session));
        return new ResultBean();
    }

    @PostMapping("/v04/setUser")
    public ResultBean setUser(HttpSession session, Needuser needuser){
        needUserService.setUser(User.getId(session),needuser);
        return new ResultBean();
    }

    @RequestMapping("v04/getUser")
    public ResultBean getUser(HttpSession session) {
        return new ResultBean(needUserService.getUser(User.getId(session)));
    }



    /**
     *
     * @param username 用户名
     * @param password 密码
     * @return null
     */
    @PostMapping("/v3/login")
    public ResultBean login(HttpSession session, String username, String password) {
        skillUserService.selectByPassword(username,password,session);
        return new ResultBean();
    }

    /**
     * @return 当前用户详细信息
     */
    @RequestMapping("/v03/getUserInfo")
    public ResultBean getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return  new ResultBean(voService.selectSkillUserById(user.getId()));
    }

    /**
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    @PostMapping("/v03/setPassword")
    public ResultBean setPassword(String oldPassword, String newPassword, HttpSession session){
        User user = (User) session.getAttribute("user");
        skillUserService.setPassword(oldPassword,newPassword,user.getId());
        return new ResultBean();
    }

    /**
     *
     * @param skilluser 用户的部分资料
     * @param skillusers 用户的部分资料
     * @param pic
     * @param session
     * @return
     * @throws IOException
     */
    @PostMapping("/v03/setUser")
    public ResultBean setUser(Skilluser skilluser, Skillusers skillusers,MultipartFile pic, HttpSession session) throws IOException {
        skillUserService.setUser(skilluser,skillusers,pic,session,User.getId(session));
        return new ResultBean();
    }
}
